import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:situation/app/theme/gaps.dart';
import 'package:situation/data/store/config_store.dart';
import 'package:situation/domain/chat/concat_list_domain.dart';
import 'package:situation/domain/chat/conversation_msg.dart';
import 'package:situation/resources/assets.dart';
import 'package:situation/widgets/app/app_title.dart';
import 'package:situation/widgets/mk_load_image.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';
import 'state.dart';

class CommunicationConcatListPage extends StatefulWidget {
  const CommunicationConcatListPage({Key? key}) : super(key: key);

  @override
  State<CommunicationConcatListPage> createState() =>
      _CommunicationConcatListPageState();
}

class _CommunicationConcatListPageState
    extends State<CommunicationConcatListPage> {
  final CommunicationConcatListController controller = Get.put(
    CommunicationConcatListController(),
  );
  final CommunicationConcatListState state =
      Get.find<CommunicationConcatListController>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CommunicationConcatListController>(
      builder: (_) {
        return Scaffold(
          appBar: TDNavBar(title: "通讯录" + state.connectionStatusStr),
          body: ListView.builder(
            itemCount: state.orgList.length,
            itemBuilder: (context, index) {
              return Column(
                children: [
                  TDCell(
                    titleWidget: Row(
                      children: [
                        MkLoadImage(Assets.commonOrg, width: 26),
                        Gaps.hGap8,
                        Text(
                          '${state.orgList[index].name}',
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                  ),

                  ...state.orgList[index].children.map<Widget>((e) {
                    return TDCell(
                      onClick: (v) {
                        controller.onTap(e);
                      },
                      arrow: true,
                      titleWidget: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            "${e.name}",
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(fontSize: 16),
                          ),
                          Gaps.vGap4,
                          _getText(e),
                        ],
                      ),
                      noteWidget: Row(children: []),
                      imageWidget: Stack(
                        children: [
                          TDAvatar(
                            avatarUrl:
                                ConfigStore.to.baseFileUrl + '/' + e.headImgUrl,
                          ),
                          Positioned(
                            right: 0,
                            bottom: 0,
                            child: Icon(
                              Icons.circle,
                              color:
                                  e.online == 1 ? Colors.green : Colors.red,
                              size: 12,
                            ),
                          ),
                        ],
                      ),
                    );
                  }).toList(),
                ],
              );
            },
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<CommunicationConcatListController>();
    super.dispose();
  }

  _getText(OrgChildItem e) {
    UIConversation? uiConversation = state.msgPut[e.id];

    if (uiConversation != null) {
      return Text(
        getShowContent(uiConversation),
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(fontSize: 12, color: Colors.grey),
      );
    }
    return Text("");
  }

  String getShowContent(UIConversation uiConversation) {
    if (uiConversation.lastContent == '') {
      uiConversation.msg.getWkMsg().then((value) {
        if (value != null && value.messageContent != null) {
          uiConversation.lastContent = value.messageContent!.displayText();
          setState(() {});
        }
      });
      return '';
    }
    return uiConversation.lastContent;
  }
}
